home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / djgpp / src / gas-211 / gas / targ-cpu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  68.4 KB  |  2,556 lines

  1. /* i386.c -- Assemble code for the Intel 80386
  2.    Copyright (C) 1989, 1991, 1992 Free Software Foundation.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.   Intel 80386 machine specific gas.
  22.   Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
  23.   Bugs & suggestions are completely welcome.  This is free software.
  24.   Please help us make it better.
  25.   */
  26.  
  27. #include <ctype.h>
  28.  
  29. #include "as.h"
  30.  
  31. #include "obstack.h"
  32. #include "opcode/i386.h"
  33.  
  34. /* 'md_assemble ()' gathers together information and puts it into a
  35.    i386_insn. */
  36.  
  37. typedef struct
  38.   {
  39.     /* TM holds the template for the insn were currently assembling. */
  40.     template tm;
  41.     /* SUFFIX holds the opcode suffix (e.g. 'l' for 'movl') if given. */
  42.     char suffix;
  43.     /* Operands are coded with OPERANDS, TYPES, DISPS, IMMS, and REGS. */
  44.  
  45.     /* OPERANDS gives the number of given operands. */
  46.     unsigned int operands;
  47.  
  48.     /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
  49.        of given register, displacement, memory operands and immediate
  50.        operands. */
  51.     unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
  52.  
  53.     /* TYPES [i] is the type (see above #defines) which tells us how to
  54.        search through DISPS [i] & IMMS [i] & REGS [i] for the required
  55.        operand.  */
  56.     unsigned int types[MAX_OPERANDS];
  57.  
  58.     /* Displacements (if given) for each operand. */
  59.     expressionS *disps[MAX_OPERANDS];
  60.  
  61.     /* Immediate operands (if given) for each operand. */
  62.     expressionS *imms[MAX_OPERANDS];
  63.  
  64.     /* Register operands (if given) for each operand. */
  65.     reg_entry *regs[MAX_OPERANDS];
  66.  
  67.     /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
  68.        the base index byte below.  */
  69.     reg_entry *base_reg;
  70.     reg_entry *index_reg;
  71.     unsigned int log2_scale_factor;
  72.  
  73.     /* SEG gives the seg_entry of this insn.  It is equal to zero unless
  74.        an explicit segment override is given. */
  75.     const seg_entry *seg;    /* segment for memory operands (if given) */
  76.  
  77.     /* PREFIX holds all the given prefix opcodes (usually null).
  78.        PREFIXES is the size of PREFIX. */
  79.     /* richfix: really unsigned? */
  80.     unsigned char prefix[MAX_PREFIXES];
  81.     unsigned int prefixes;
  82.  
  83.     /* RM and IB are the modrm byte and the base index byte where the
  84.        addressing modes of this insn are encoded. */
  85.  
  86.     modrm_byte rm;
  87.     base_index_byte bi;
  88.   }
  89.  
  90. i386_insn;
  91.  
  92. /* This array holds the chars that always start a comment.  If the
  93.    pre-processor is disabled, these aren't very useful */
  94. #ifdef TE_I386AIX
  95. const char comment_chars[] = "#/";
  96. #else
  97. const char comment_chars[] = "#";
  98. #endif
  99.  
  100. /* This array holds the chars that only start a comment at the beginning of
  101.    a line.  If the line seems to have the form '# 123 filename'
  102.    .line and .file directives will appear in the pre-processed output */
  103. /* Note that input_file.c hand checks for '#' at the beginning of the
  104.    first line of the input file.  This is because the compiler outputs
  105.    #NO_APP at the beginning of its output. */
  106. /* Also note that comments started like this one will always work if
  107.    '/' isn't otherwise defined. */
  108. const char line_comment_chars[] = "/";    /* removed '#' xoxorich. */
  109. const char line_separator_chars[] = "";
  110.  
  111. /* Chars that can be used to separate mant from exp in floating point nums */
  112. const char EXP_CHARS[] = "eE";
  113.  
  114. /* Chars that mean this number is a floating point constant */
  115. /* As in 0f12.456 */
  116. /* or    0d1.2345e12 */
  117. const char FLT_CHARS[] = "fFdDxX";
  118.  
  119. /* tables for lexical analysis */
  120. static char opcode_chars[256];
  121. static char register_chars[256];
  122. static char operand_chars[256];
  123. static char space_chars[256];
  124. static char identifier_chars[256];
  125. static char digit_chars[256];
  126.  
  127. /* lexical macros */
  128. #define is_opcode_char(x) (opcode_chars[(unsigned char) x])
  129. #define is_operand_char(x) (operand_chars[(unsigned char) x])
  130. #define is_register_char(x) (register_chars[(unsigned char) x])
  131. #define is_space_char(x) (space_chars[(unsigned char) x])
  132. #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
  133. #define is_digit_char(x) (digit_chars[(unsigned char) x])
  134.  
  135. /* put here all non-digit non-letter charcters that may occur in an operand */
  136. static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:";
  137.  
  138. static char *ordinal_names[] =
  139. {"first", "second", "third"};    /* for printfs */
  140.  
  141. /* md_assemble() always leaves the strings it's passed unaltered.  To
  142.    effect this we maintain a stack of saved characters that we've smashed
  143.    with '\0's (indicating end of strings for various sub-fields of the
  144.    assembler instruction). */
  145. static char save_stack[32];
  146. static char *save_stack_p;    /* stack pointer */
  147. #define END_STRING_AND_SAVE(s)      *save_stack_p++ = *s; *s = '\0'
  148. #define RESTORE_END_STRING(s)       *s = *--save_stack_p
  149.  
  150. /* The instruction we're assembling. */
  151. static i386_insn i;
  152.  
  153. /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
  154. static expressionS disp_expressions[2], im_expressions[2];
  155.  
  156. /* pointers to ebp & esp entries in reg_hash hash table */
  157. static reg_entry *ebp, *esp;
  158.  
  159. static int this_operand;    /* current operand we are working on */
  160.  
  161. /*
  162.   Interface to relax_segment.
  163.   There are 2 relax states for 386 jump insns: one for conditional & one
  164.   for unconditional jumps.  This is because the these two types of jumps
  165.   add different sizes to frags when we're figuring out what sort of jump
  166.   to choose to reach a given label.  */
  167.  
  168. /* types */
  169. #define COND_JUMP 1        /* conditional jump */
  170. #define UNCOND_JUMP 2        /* unconditional jump */
  171. /* sizes */
  172. #define BYTE 0
  173. #define WORD 1
  174. #define DWORD 2
  175. #define UNKNOWN_SIZE 3
  176.  
  177. #define ENCODE_RELAX_STATE(type,size) ((type<<2) | (size))
  178. #define SIZE_FROM_RELAX_STATE(s) \
  179.     ( (((s) & 0x3) == BYTE ? 1 : (((s) & 0x3) == WORD ? 2 : 4)) )
  180.  
  181. const relax_typeS md_relax_table[] =
  182. {
  183. /* The fields are:
  184.    1) most positive reach of this state,
  185.    2) most negative reach of this state,
  186.    3) how many bytes this mode will add to the size of the current frag
  187.    4) which index into the table to try if we can't fit into this one.
  188.    */
  189.   {1, 1, 0, 0},
  190.   {1, 1, 0, 0},
  191.   {1, 1, 0, 0},
  192.   {1, 1, 0, 0},
  193.  
  194. /* For now we don't use word displacement jumps:  they may be
  195.    untrustworthy. */
  196.   {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
  197. /* word conditionals add 3 bytes to frag:
  198.    2 opcode prefix; 1 displacement bytes */
  199.   {32767 + 2, -32768 + 2, 3, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
  200. /* dword conditionals adds 4 bytes to frag:
  201.    1 opcode prefix; 3 displacement bytes */
  202.   {0, 0, 4, 0},
  203.   {1, 1, 0, 0},
  204.  
  205.   {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
  206. /* word jmp adds 2 bytes to frag:
  207.    1 opcode prefix; 1 displacement bytes */
  208.   {32767 + 2, -32768 + 2, 2, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
  209. /* dword jmp adds 3 bytes to frag:
  210.    0 opcode prefix; 3 displacement bytes */
  211.   {0, 0, 3, 0},
  212.   {1, 1, 0, 0},
  213.  
  214. };
  215.  
  216. static char *output_invalid PARAMS ((int c));
  217. static int i386_operand PARAMS ((char *operand_string));
  218. static reg_entry *parse_register PARAMS ((char *reg_string));
  219. #ifndef I386COFF
  220. static void s_bss PARAMS ((void));
  221. #endif
  222.  
  223. static unsigned long
  224. mode_from_disp_size (t)
  225.      unsigned long t;
  226. {
  227.   return ((t & (Disp8))
  228.       ? 1
  229.       : ((t & (Disp32)) ? 2 : 0));
  230. }                /* mode_from_disp_size() */
  231.  
  232. /* convert opcode suffix ('b' 'w' 'l' typically) into type specifyer */
  233.  
  234. static unsigned long
  235. opcode_suffix_to_type (s)
  236.      unsigned long s;
  237. {
  238.   return (s == BYTE_OPCODE_SUFFIX
  239.       ? Byte : (s == WORD_OPCODE_SUFFIX
  240.             ? Word : DWord));
  241. }                /* opcode_suffix_to_type() */
  242.  
  243. static int
  244. fits_in_signed_byte (num)
  245.      long num;
  246. {
  247.   return ((num >= -128) && (num <= 127));
  248. }                /* fits_in_signed_byte() */
  249.  
  250. static int
  251. fits_in_unsigned_byte (num)
  252.      long num;
  253. {
  254.   return ((num & 0xff) == num);
  255. }                /* fits_in_unsigned_byte() */
  256.  
  257. static int
  258. fits_in_unsigned_word (num)
  259.      long num;
  260. {
  261.   return ((num & 0xffff) == num);
  262. }                /* fits_in_unsigned_word() */
  263.  
  264. static int
  265. fits_in_signed_word (num)
  266.      long num;
  267. {
  268.   return ((-32768 <= num) && (num <= 32767));
  269. }                /* fits_in_signed_word() */
  270.  
  271. static int
  272. smallest_imm_type (num)
  273.      long num;
  274. {
  275.   return ((num == 1)
  276.       ? (Imm1 | Imm8 | Imm8S | Imm16 | Imm32)
  277.       : (fits_in_signed_byte (num)
  278.          ? (Imm8S | Imm8 | Imm16 | Imm32)
  279.          : (fits_in_unsigned_byte (num)
  280.         ? (Imm8 | Imm16 | Imm32)
  281.         : ((fits_in_signed_word (num) || fits_in_unsigned_word (num))
  282.            ? (Imm16 | Imm32)
  283.            : (Imm32)))));
  284. }                /* smallest_imm_type() */
  285.  
  286. /* Ignore certain directives generated by gcc. This probably should
  287.    not be here. */
  288. void
  289. dummy ()
  290. {
  291.   while (*input_line_pointer && *input_line_pointer != '\n')
  292.     input_line_pointer++;
  293. }
  294.  
  295. const pseudo_typeS md_pseudo_table[] =
  296. {
  297. #ifndef I386COFF
  298.   {"bss", s_bss, 0},
  299. #endif
  300. #if !defined (TE_LINUX) && !defined (TE_386BSD)
  301.   {"align", s_align_bytes, 0},
  302. #else
  303.   {"align", s_align_ptwo, 0},
  304. #endif
  305.   {"ffloat", float_cons, 'f'},
  306.   {"dfloat", float_cons, 'd'},
  307.   {"tfloat", float_cons, 'x'},
  308.   {"value", cons, 2},
  309.   {"noopt", s_ignore, 0},
  310.   {"optim", s_ignore, 0},
  311.   {0, 0, 0}
  312. };
  313.  
  314. /* for interface with expression () */
  315. extern char *input_line_pointer;
  316.  
  317. /* obstack for constructing various things in md_begin */
  318. struct obstack o;
  319.  
  320. /* hash table for opcode lookup */
  321. static struct hash_control *op_hash = (struct hash_control *) 0;
  322. /* hash table for register lookup */
  323. static struct hash_control *reg_hash = (struct hash_control *) 0;
  324. /* hash table for prefix lookup */
  325. static struct hash_control *prefix_hash = (struct hash_control *) 0;
  326.  
  327.  
  328. void
  329. md_begin ()
  330. {
  331.   char *hash_err;
  332.  
  333.   obstack_begin (&o, 4096);
  334.  
  335.   /* initialize op_hash hash table */
  336.   op_hash = hash_new ();    /* xmalloc handles error */
  337.  
  338.   {
  339.     register const template *optab;
  340.     register templates *core_optab;
  341.     char *prev_name;
  342.  
  343.     optab = i386_optab;        /* setup for loop */
  344.     prev_name = optab->name;
  345.     obstack_grow (&o, optab, sizeof (template));
  346.     core_optab = (templates *) xmalloc (sizeof (templates));
  347.  
  348.     for (optab++; optab < i386_optab_end; optab++)
  349.       {
  350.     if (!strcmp (optab->name, prev_name))
  351.       {
  352.         /* same name as before --> append to current template list */
  353.         obstack_grow (&o, optab, sizeof (template));
  354.       }
  355.     else
  356.       {
  357.         /* different name --> ship out current template list;
  358.            add to hash table; & begin anew */
  359.         /* Note: end must be set before start! since obstack_next_free
  360.            changes upon opstack_finish */
  361.         core_optab->end = (template *) obstack_next_free (&o);
  362.         core_optab->start = (template *) obstack_finish (&o);
  363.         hash_err = hash_insert (op_hash, prev_name, (char *) core_optab);
  364.         if (hash_err && *hash_err)
  365.           {
  366.           hash_error:
  367.         as_fatal ("Internal Error:  Can't hash %s: %s", prev_name, hash_err);
  368.           }
  369.         prev_name = optab->name;
  370.         core_optab = (templates *) xmalloc (sizeof (templates));
  371.         obstack_grow (&o, optab, sizeof (template));
  372.       }
  373.       }
  374.   }
  375.  
  376.   /* initialize reg_hash hash table */
  377.   reg_hash = hash_new ();
  378.   {
  379.     register const reg_entry *regtab;
  380.  
  381.     for (regtab = i386_regtab; regtab < i386_regtab_end; regtab++)
  382.       {
  383.     hash_err = hash_insert (reg_hash, regtab->reg_name, regtab);
  384.     if (hash_err && *hash_err)
  385.       goto hash_error;
  386.       }
  387.   }
  388.  
  389.   esp = (reg_entry *) hash_find (reg_hash, "esp");
  390.   ebp = (reg_entry *) hash_find (reg_hash, "ebp");
  391.  
  392.   /* initialize reg_hash hash table */
  393.   prefix_hash = hash_new ();
  394.   {
  395.     register const prefix_entry *prefixtab;
  396.  
  397.     for (prefixtab = i386_prefixtab;
  398.      prefixtab < i386_prefixtab_end; prefixtab++)
  399.       {
  400.     hash_err = hash_insert (prefix_hash, prefixtab->prefix_name, prefixtab);
  401.     if (hash_err && *hash_err)
  402.       goto hash_error;
  403.       }
  404.   }
  405.  
  406.   /* fill in lexical tables:  opcode_chars, operand_chars, space_chars */
  407.   {
  408.     register unsigned int c;
  409.  
  410.     memset (opcode_chars, '\0', sizeof (opcode_chars));
  411.     memset (operand_chars, '\0', sizeof (operand_chars));
  412.     memset (space_chars, '\0', sizeof (space_chars));
  413.     memset (identifier_chars, '\0', sizeof (identifier_chars));
  414.     memset (digit_chars, '\0', sizeof (digit_chars));
  415.  
  416.     for (c = 0; c < 256; c++)
  417.       {
  418.     if (islower (c) || isdigit (c))
  419.       {
  420.         opcode_chars[c] = c;
  421.         register_chars[c] = c;
  422.       }
  423.     else if (isupper (c))
  424.       {
  425.         opcode_chars[c] = tolower (c);
  426.         register_chars[c] = opcode_chars[c];
  427.       }
  428.     else if (c == PREFIX_SEPERATOR)
  429.       {
  430.         opcode_chars[c] = c;
  431.       }
  432.     else if (c == ')' || c == '(')
  433.       {
  434.         register_chars[c] = c;
  435.       }
  436.  
  437.     if (isupper (c) || islower (c) || isdigit (c))
  438.       operand_chars[c] = c;
  439.     else if (c && strchr (operand_special_chars, c))
  440.       operand_chars[c] = c;
  441.  
  442.     if (isdigit (c) || c == '-')
  443.       digit_chars[c] = c;
  444.  
  445.     if (isalpha (c) || c == '_' || c == '.' || isdigit (c))
  446.       identifier_chars[c] = c;
  447.  
  448.     if (c == ' ' || c == '\t')
  449.       space_chars[c] = c;
  450.       }
  451.   }
  452. }
  453.  
  454. void
  455. md_end ()
  456. {
  457. }                /* not much to do here. */
  458.  
  459.  
  460. #ifdef DEBUG386
  461.  
  462. /* debugging routines for md_assemble */
  463. /* static void pi (), pte (), pt (), pe (), ps (); */
  464.  
  465. static void
  466. pi (line, x)
  467.      char *line;
  468.      i386_insn *x;
  469. {
  470.   register template *p;
  471.   int i;
  472.  
  473.   fprintf (stdout, "%s: template ", line);
  474.   pte (&x->tm);
  475.   fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x",
  476.        x->rm.mode, x->rm.reg, x->rm.regmem);
  477.   fprintf (stdout, " base %x  index %x  scale %x\n",
  478.        x->bi.base, x->bi.index, x->bi.scale);
  479.   for (i = 0; i < x->operands; i++)
  480.     {
  481.       fprintf (stdout, "    #%d:  ", i + 1);
  482.       pt (x->types[i]);
  483.       fprintf (stdout, "\n");
  484.       if (x->types[i] & Reg)
  485.     fprintf (stdout, "%s\n", x->regs[i]->reg_name);
  486.       if (x->types[i] & Imm)
  487.     pe (x->imms[i]);
  488.       if (x->types[i] & (Disp | Abs))
  489.     pe (x->disps[i]);
  490.     }
  491. }
  492.  
  493. static void
  494. pte (t)
  495.      template *t;
  496. {
  497.   int i;
  498.   fprintf (stdout, " %d operands ", t->operands);
  499.   fprintf (stdout, "opcode %x ",
  500.        t->base_opcode);
  501.   if (t->extension_opcode != None)
  502.     fprintf (stdout, "ext %x ", t->extension_opcode);
  503.   if (t->opcode_modifier & D)
  504.     fprintf (stdout, "D");
  505.   if (t->opcode_modifier & W)
  506.     fprintf (stdout, "W");
  507.   fprintf (stdout, "\n");
  508.   for (i = 0; i < t->operands; i++)
  509.     {
  510.       fprintf (stdout, "    #%d type ", i + 1);
  511.       pt (t->operand_types[i]);
  512.       fprintf (stdout, "\n");
  513.     }
  514. }
  515.  
  516. static void
  517. pe (e)
  518.      expressionS *e;
  519. {
  520.   fprintf (stdout, "    segment       %s\n", segment_name (e->X_seg));
  521.   fprintf (stdout, "    add_number    %d (%x)\n",
  522.        e->X_add_number, e->X_add_number);
  523.   if (e->X_add_symbol)
  524.     {
  525.       fprintf (stdout, "    add_symbol    ");
  526.       ps (e->X_add_symbol);
  527.       fprintf (stdout, "\n");
  528.     }
  529.   if (e->X_subtract_symbol)
  530.     {
  531.       fprintf (stdout, "    sub_symbol    ");
  532.       ps (e->X_subtract_symbol);
  533.       fprintf (stdout, "\n");
  534.     }
  535. }
  536.  
  537. static void
  538. ps (s)
  539.      symbolS *s;
  540. {
  541.   fprintf (stdout, "%s type %s%s",
  542.        S_GET_NAME (s),
  543.        S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
  544.        segment_name (S_GET_SEGMENT (s)));
  545. }
  546.  
  547. struct type_name
  548.   {
  549.     unsigned int mask;
  550.     char *tname;
  551.   }
  552.  
  553. type_names[] =
  554. {
  555.   { Reg8, "r8" },
  556.   { Reg16, "r16" },
  557.   { Reg32, "r32" },
  558.   { Imm8, "i8" },
  559.   { Imm8S, "i8s" },
  560.   { Imm16, "i16" },
  561.   { Imm32, "i32" },
  562.   { Mem8, "Mem8" },
  563.   { Mem16, "Mem16" },
  564.   { Mem32, "Mem32" },
  565.   { BaseIndex, "BaseIndex" },
  566.   { Abs8, "Abs8" },
  567.   { Abs16, "Abs16" },
  568.   { Abs32, "Abs32" },
  569.   { Disp8, "d8" },
  570.   { Disp16, "d16" },
  571.   { Disp32, "d32" },
  572.   { SReg2, "SReg2" },
  573.   { SReg3, "SReg3" },
  574.   { Acc, "Acc" },
  575.   { InOutPortReg, "InOutPortReg" },
  576.   { ShiftCount, "ShiftCount" },
  577.   { Imm1, "i1" },
  578.   { Control, "control reg" },
  579.   { Test, "test reg" },
  580.   { FloatReg, "FReg" },
  581.   { FloatAcc, "FAcc" },
  582.   { JumpAbsolute, "Jump Absolute" },
  583.   { 0, "" }
  584. };
  585.  
  586. static void
  587. pt (t)
  588.      unsigned int t;
  589. {
  590.   register struct type_name *ty;
  591.  
  592.   if (t == Unknown)
  593.     {
  594.       fprintf (stdout, "Unknown");
  595.     }
  596.   else
  597.     {
  598.       for (ty = type_names; ty->mask; ty++)
  599.     if (t & ty->mask)
  600.       fprintf (stdout, "%s, ", ty->tname);
  601.     }
  602.   fflush (stdout);
  603. }
  604.  
  605. #endif /* DEBUG386 */
  606.  
  607. #ifdef BFD_ASSEMBLER
  608. static bfd_reloc_code_real_type
  609. reloc (size, pcrel)
  610.      int size;
  611.      int pcrel;
  612. {
  613.   if (pcrel)
  614.     switch (size)
  615.       {
  616.       case 1: return BFD_RELOC_8_PCREL;
  617.       case 2: return BFD_RELOC_16_PCREL;
  618.       case 4: return BFD_RELOC_32_PCREL;
  619.       }
  620.   else
  621.     switch (size)
  622.       {
  623.       case 1: return BFD_RELOC_8;
  624.       case 2: return BFD_RELOC_16;
  625.       case 4: return BFD_RELOC_32;
  626.       }
  627.   abort ();
  628. }
  629. #endif
  630.  
  631. /* This is the guts of the machine-dependent assembler.  LINE points to a
  632.    machine dependent instruction.  This funciton is supposed to emit
  633.    the frags/bytes it assembles to.  */
  634. void
  635. md_assemble (line)
  636.      char *line;
  637. {
  638.   /* Holds temlate once we've found it. */
  639.   register template *t;
  640.  
  641.   /* Possible templates for current insn */
  642.   templates *current_templates = (templates *) 0;
  643.  
  644.   /* Initialize globals. */
  645.   memset (&i, '\0', sizeof (i));
  646.   memset (disp_expressions, '\0', sizeof (disp_expressions));
  647.   memset (im_expressions, '\0', sizeof (im_expressions));
  648.   save_stack_p = save_stack;    /* reset stack pointer */
  649.  
  650.   /* Fist parse an opcode & call i386_operand for the operands.
  651.      We assume that the scrubber has arranged it so that line[0] is the valid
  652.      start of a (possibly prefixed) opcode. */
  653.   {
  654.     register char *l = line;    /* Fast place to put LINE. */
  655.  
  656.     /* 1 if operand is pending after ','. */
  657.     unsigned int expecting_operand = 0;
  658.     /* 1 if we found a prefix only acceptable with string insns. */
  659.     unsigned int expecting_string_instruction = 0;
  660.     /* Non-zero if operand parens not balenced. */
  661.     unsigned int paren_not_balenced;
  662.     char *token_start = l;
  663.  
  664.     while (!is_space_char (*l) && *l != END_OF_INSN)
  665.       {
  666.     if (!is_opcode_char (*l))
  667.       {
  668.         as_bad ("invalid character %s in opcode", output_invalid (*l));
  669.         return;
  670.       }
  671.     else if (*l != PREFIX_SEPERATOR)
  672.       {
  673.         *l = opcode_chars[(unsigned char) *l];    /* fold case of opcodes */
  674.         l++;
  675.       }
  676.     else
  677.       {            /* this opcode's got a prefix */
  678.         register unsigned int q;
  679.         register prefix_entry *prefix;
  680.  
  681.         if (l == token_start)
  682.           {
  683.         as_bad ("expecting prefix; got nothing");
  684.         return;
  685.           }
  686.         END_STRING_AND_SAVE (l);
  687.         prefix = (prefix_entry *) hash_find (prefix_hash, token_start);
  688.         if (!prefix)
  689.           {
  690.         as_bad ("no such opcode prefix ('%s')", token_start);
  691.         return;
  692.           }
  693.         RESTORE_END_STRING (l);
  694.         /* check for repeated prefix */
  695.         for (q = 0; q < i.prefixes; q++)
  696.           if (i.prefix[q] == prefix->prefix_code)
  697.         {
  698.           as_bad ("same prefix used twice; you don't really want this!");
  699.           return;
  700.         }
  701.         if (i.prefixes == MAX_PREFIXES)
  702.           {
  703.         as_bad ("too many opcode prefixes");
  704.         return;
  705.           }
  706.         i.prefix[i.prefixes++] = prefix->prefix_code;
  707.         if (prefix->prefix_code == REPE || prefix->prefix_code == REPNE)
  708.           expecting_string_instruction = 1;
  709.         /* skip past PREFIX_SEPERATOR and reset token_start */
  710.         token_start = ++l;
  711.       }
  712.       }
  713.     END_STRING_AND_SAVE (l);
  714.     if (token_start == l)
  715.       {
  716.     as_bad ("expecting opcode; got nothing");
  717.     return;
  718.       }
  719.  
  720.     /* Lookup insn in hash; try intel & att naming conventions if appropriate;
  721.        that is:  we only use the opcode suffix 'b' 'w' or 'l' if we need to. */
  722.     current_templates = (templates *) hash_find (op_hash, token_start);
  723.     if (!current_templates)
  724.       {
  725.     int last_index = strlen (token_start) - 1;
  726.     char last_char = token_start[last_index];
  727.     switch (last_char)
  728.       {
  729.       case DWORD_OPCODE_SUFFIX:
  730.       case WORD_OPCODE_SUFFIX:
  731.       case BYTE_OPCODE_SUFFIX:
  732.         token_start[last_index] = '\0';
  733.         current_templates = (templates *) hash_find (op_hash, token_start);
  734.         token_start[last_index] = last_char;
  735.         i.suffix = last_char;
  736.       }
  737.     if (!current_templates)
  738.       {
  739.         as_bad ("no such 386 instruction: `%s'", token_start);
  740.         return;
  741.       }
  742.       }
  743.     RESTORE_END_STRING (l);
  744.  
  745.     /* check for rep/repne without a string instruction */
  746.     if (expecting_string_instruction &&
  747.     !IS_STRING_INSTRUCTION (current_templates->
  748.                 start->base_opcode))
  749.       {
  750.     as_bad ("expecting string instruction after rep/repne");
  751.     return;
  752.       }
  753.  
  754.     /* There may be operands to parse. */
  755.     if (*l != END_OF_INSN &&
  756.     /* For string instructions, we ignore any operands if given.  This
  757.        kludges, for example, 'rep/movsb %ds:(%esi), %es:(%edi)' where
  758.        the operands are always going to be the same, and are not really
  759.        encoded in machine code. */
  760.     !IS_STRING_INSTRUCTION (current_templates->
  761.                 start->base_opcode))
  762.       {
  763.     /* parse operands */
  764.     do
  765.       {
  766.         /* skip optional white space before operand */
  767.         while (!is_operand_char (*l) && *l != END_OF_INSN)
  768.           {
  769.         if (!is_space_char (*l))
  770.           {
  771.             as_bad ("invalid character %s before %s operand",
  772.                 output_invalid (*l),
  773.                 ordinal_names[i.operands]);
  774.             return;
  775.           }
  776.         l++;
  777.           }
  778.         token_start = l;    /* after white space */
  779.         paren_not_balenced = 0;
  780.         while (paren_not_balenced || *l != ',')
  781.           {
  782.         if (*l == END_OF_INSN)
  783.           {
  784.             if (paren_not_balenced)
  785.               {
  786.             as_bad ("unbalenced parenthesis in %s operand.",
  787.                 ordinal_names[i.operands]);
  788.             return;
  789.               }
  790.             else
  791.               break;    /* we are done */
  792.           }
  793.         else if (!is_operand_char (*l))
  794.           {
  795.             as_bad ("invalid character %s in %s operand",
  796.                 output_invalid (*l),
  797.                 ordinal_names[i.operands]);
  798.             return;
  799.           }
  800.         if (*l == '(')
  801.           ++paren_not_balenced;
  802.         if (*l == ')')
  803.           --paren_not_balenced;
  804.         l++;
  805.           }
  806.         if (l != token_start)
  807.           {            /* yes, we've read in another operand */
  808.         unsigned int operand_ok;
  809.         this_operand = i.operands++;
  810.         if (i.operands > MAX_OPERANDS)
  811.           {
  812.             as_bad ("spurious operands; (%d operands/instruction max)",
  813.                 MAX_OPERANDS);
  814.             return;
  815.           }
  816.         /* now parse operand adding info to 'i' as we go along */
  817.         END_STRING_AND_SAVE (l);
  818.         operand_ok = i386_operand (token_start);
  819.         RESTORE_END_STRING (l);    /* restore old contents */
  820.         if (!operand_ok)
  821.           return;
  822.           }
  823.         else
  824.           {
  825.         if (expecting_operand)
  826.           {
  827.           expecting_operand_after_comma:
  828.             as_bad ("expecting operand after ','; got nothing");
  829.             return;
  830.           }
  831.         if (*l == ',')
  832.           {
  833.             as_bad ("expecting operand before ','; got nothing");
  834.             return;
  835.           }
  836.           }
  837.  
  838.         /* now *l must be either ',' or END_OF_INSN */
  839.         if (*l == ',')
  840.           {
  841.         if (*++l == END_OF_INSN)
  842.           {        /* just skip it, if it's \n complain */
  843.             goto expecting_operand_after_comma;
  844.           }
  845.         expecting_operand = 1;
  846.           }
  847.       }
  848.     while (*l != END_OF_INSN);    /* until we get end of insn */
  849.       }
  850.   }
  851.  
  852.   /* Now we've parsed the opcode into a set of templates, and have the
  853.      operands at hand.
  854.  
  855.      Next, we find a template that matches the given insn,
  856.      making sure the overlap of the given operands types is consistent
  857.      with the template operand types. */
  858.  
  859. #define MATCH(overlap,given_type) \
  860.     (overlap && \
  861.      (((overlap & (JumpAbsolute|BaseIndex|Mem8)) \
  862.        == (given_type & (JumpAbsolute|BaseIndex|Mem8))) \
  863.       || (overlap == InOutPortReg)))
  864.  
  865.  
  866.   /* If m0 and m1 are register matches they must be consistent
  867.      with the expected operand types t0 and t1.
  868.      That is, if both m0 & m1 are register matches
  869.      i.e. ( ((m0 & (Reg)) && (m1 & (Reg)) ) ?
  870.      then, either 1. or 2. must be true:
  871.      1. the expected operand type register overlap is null:
  872.      (t0 & t1 & Reg) == 0
  873.      AND
  874.      the given register overlap is null:
  875.      (m0 & m1 & Reg) == 0
  876.      2. the expected operand type register overlap == the given
  877.      operand type overlap:  (t0 & t1 & m0 & m1 & Reg).
  878.      */
  879. #define CONSISTENT_REGISTER_MATCH(m0, m1, t0, t1) \
  880.         ( ((m0 & (Reg)) && (m1 & (Reg))) ? \
  881.          ( ((t0 & t1 & (Reg)) == 0 && (m0 & m1 & (Reg)) == 0) || \
  882.           ((t0 & t1) & (m0 & m1) & (Reg)) \
  883.           ) : 1)
  884.   {
  885.     register unsigned int overlap0, overlap1;
  886.     expressionS *exp;
  887.     unsigned int overlap2;
  888.     unsigned int found_reverse_match;
  889.  
  890.     overlap0 = overlap1 = overlap2 = found_reverse_match = 0;
  891.     for (t = current_templates->start;
  892.      t < current_templates->end;
  893.      t++)
  894.       {
  895.  
  896.     /* must have right number of operands */
  897.     if (i.operands != t->operands)
  898.       continue;
  899.     else if (!t->operands)
  900.       break;        /* 0 operands always matches */
  901.  
  902.     overlap0 = i.types[0] & t->operand_types[0];
  903.     switch (t->operands)
  904.       {
  905.       case 1:
  906.         if (!MATCH (overlap0, i.types[0]))
  907.           continue;
  908.         break;
  909.       case 2:
  910.       case 3:
  911.         overlap1 = i.types[1] & t->operand_types[1];
  912.         if (!MATCH (overlap0, i.types[0]) ||
  913.         !MATCH (overlap1, i.types[1]) ||
  914.         !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
  915.                         t->operand_types[0],
  916.                         t->operand_types[1]))
  917.           {
  918.  
  919.         /* check if other direction is valid ... */
  920.         if (!(t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS))
  921.           continue;
  922.  
  923.         /* try reversing direction of operands */
  924.         overlap0 = i.types[0] & t->operand_types[1];
  925.         overlap1 = i.types[1] & t->operand_types[0];
  926.         if (!MATCH (overlap0, i.types[0]) ||
  927.             !MATCH (overlap1, i.types[1]) ||
  928.             !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
  929.                         t->operand_types[0],
  930.                         t->operand_types[1]))
  931.           {
  932.             /* does not match either direction */
  933.             continue;
  934.           }
  935.         /* found a reverse match here -- slip through */
  936.         /* found_reverse_match holds which of D or FloatD we've found */
  937.         found_reverse_match = t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS;
  938.           }            /* endif: not forward match */
  939.         /* found either forward/reverse 2 operand match here */
  940.         if (t->operands == 3)
  941.           {
  942.         overlap2 = i.types[2] & t->operand_types[2];
  943.         if (!MATCH (overlap2, i.types[2]) ||
  944.             !CONSISTENT_REGISTER_MATCH (overlap0, overlap2,
  945.                         t->operand_types[0],
  946.                         t->operand_types[2]) ||
  947.             !CONSISTENT_REGISTER_MATCH (overlap1, overlap2,
  948.                         t->operand_types[1],
  949.                         t->operand_types[2]))
  950.           continue;
  951.           }
  952.         /* found either forward/reverse 2 or 3 operand match here:
  953.            slip through to break */
  954.       }
  955.     break;            /* we've found a match; break out of loop */
  956.       }                /* for (t = ... */
  957.     if (t == current_templates->end)
  958.       {                /* we found no match */
  959.     as_bad ("operands given don't match any known 386 instruction");
  960.     return;
  961.       }
  962.  
  963.     /* Copy the template we found (we may change it!). */
  964.     memcpy (&i.tm, t, sizeof (template));
  965.     t = &i.tm;            /* alter new copy of template */
  966.  
  967.     /* If there's no opcode suffix we try to invent one based on register
  968.        operands. */
  969.     if (!i.suffix && i.reg_operands)
  970.       {
  971.     /* We take i.suffix from the LAST register operand specified.  This
  972.        assumes that the last register operands is the destination register
  973.        operand. */
  974.     int o;
  975.     for (o = 0; o < MAX_OPERANDS; o++)
  976.       if (i.types[o] & Reg)
  977.         {
  978.           i.suffix = (i.types[o] == Reg8) ? BYTE_OPCODE_SUFFIX :
  979.         (i.types[o] == Reg16) ? WORD_OPCODE_SUFFIX :
  980.         DWORD_OPCODE_SUFFIX;
  981.         }
  982.       }
  983.  
  984.     /* Make still unresolved immediate matches conform to size of immediate
  985.        given in i.suffix. Note:  overlap2 cannot be an immediate!
  986.        We assume this. */
  987.     if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32))
  988.     && overlap0 != Imm8 && overlap0 != Imm8S
  989.     && overlap0 != Imm16 && overlap0 != Imm32)
  990.       {
  991.     if (!i.suffix)
  992.       {
  993.         as_bad ("no opcode suffix given; can't determine immediate size");
  994.         return;
  995.       }
  996.     overlap0 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
  997.              (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  998.       }
  999.     if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32))
  1000.     && overlap1 != Imm8 && overlap1 != Imm8S
  1001.     && overlap1 != Imm16 && overlap1 != Imm32)
  1002.       {
  1003.     if (!i.suffix)
  1004.       {
  1005.         as_bad ("no opcode suffix given; can't determine immediate size");
  1006.         return;
  1007.       }
  1008.     overlap1 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
  1009.              (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  1010.       }
  1011.  
  1012.     i.types[0] = overlap0;
  1013.     i.types[1] = overlap1;
  1014.     i.types[2] = overlap2;
  1015.  
  1016.     if (overlap0 & ImplicitRegister)
  1017.       i.reg_operands--;
  1018.     if (overlap1 & ImplicitRegister)
  1019.       i.reg_operands--;
  1020.     if (overlap2 & ImplicitRegister)
  1021.       i.reg_operands--;
  1022.     if (overlap0 & Imm1)
  1023.       i.imm_operands = 0;    /* kludge for shift insns */
  1024.  
  1025.     if (found_reverse_match)
  1026.       {
  1027.     unsigned int save;
  1028.     save = t->operand_types[0];
  1029.     t->operand_types[0] = t->operand_types[1];
  1030.     t->operand_types[1] = save;
  1031.       }
  1032.  
  1033.     /* Finalize opcode.  First, we change the opcode based on the operand
  1034.        size given by i.suffix: we never have to change things for byte insns,
  1035.        or when no opcode suffix is need to size the operands. */
  1036.  
  1037.     if (!i.suffix && (t->opcode_modifier & W))
  1038.       {
  1039.     as_bad ("no opcode suffix given and no register operands; can't size instruction");
  1040.     return;
  1041.       }
  1042.  
  1043.     if (i.suffix && i.suffix != BYTE_OPCODE_SUFFIX)
  1044.       {
  1045.     /* Select between byte and word/dword operations. */
  1046.     if (t->opcode_modifier & W)
  1047.       t->base_opcode |= W;
  1048.     /* Now select between word & dword operations via the
  1049.                    operand size prefix. */
  1050.     if (i.suffix == WORD_OPCODE_SUFFIX)
  1051.       {
  1052.         if (i.prefixes == MAX_PREFIXES)
  1053.           {
  1054.         as_bad ("%d prefixes given and 'w' opcode suffix gives too many prefixes",
  1055.             MAX_PREFIXES);
  1056.         return;
  1057.           }
  1058.         i.prefix[i.prefixes++] = WORD_PREFIX_OPCODE;
  1059.       }
  1060.       }
  1061.  
  1062.     /* For insns with operands there are more diddles to do to the opcode. */
  1063.     if (i.operands)
  1064.       {
  1065.     /* If we found a reverse match we must alter the opcode direction bit
  1066.        found_reverse_match holds bit to set (different for int &
  1067.        float insns). */
  1068.  
  1069.     if (found_reverse_match)
  1070.       {
  1071.         t->base_opcode |= found_reverse_match;
  1072.       }
  1073.  
  1074.     /* The imul $imm, %reg instruction is converted into
  1075.        imul $imm, %reg, %reg. */
  1076.     if (t->opcode_modifier & imulKludge)
  1077.       {
  1078.         /* Pretend we saw the 3 operand case. */
  1079.         i.regs[2] = i.regs[1];
  1080.         i.reg_operands = 2;
  1081.       }
  1082.  
  1083.     /* Certain instructions expect the destination to be in the i.rm.reg
  1084.        field.  This is by far the exceptional case.  For these
  1085.        instructions, if the source operand is a register, we must reverse
  1086.        the i.rm.reg and i.rm.regmem fields.  We accomplish this by faking
  1087.        that the two register operands were given in the reverse order. */
  1088.     if ((t->opcode_modifier & ReverseRegRegmem) && i.reg_operands == 2)
  1089.       {
  1090.         unsigned int first_reg_operand = (i.types[0] & Reg) ? 0 : 1;
  1091.         unsigned int second_reg_operand = first_reg_operand + 1;
  1092.         reg_entry *tmp = i.regs[first_reg_operand];
  1093.         i.regs[first_reg_operand] = i.regs[second_reg_operand];
  1094.         i.regs[second_reg_operand] = tmp;
  1095.       }
  1096.  
  1097.     if (t->opcode_modifier & ShortForm)
  1098.       {
  1099.         /* The register or float register operand is in operand 0 or 1. */
  1100.         unsigned int o = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
  1101.         /* Register goes in low 3 bits of opcode. */
  1102.         t->base_opcode |= i.regs[o]->reg_num;
  1103.       }
  1104.     else if (t->opcode_modifier & ShortFormW)
  1105.       {
  1106.         /* Short form with 0x8 width bit.  Register is always dest. operand */
  1107.         t->base_opcode |= i.regs[1]->reg_num;
  1108.         if (i.suffix == WORD_OPCODE_SUFFIX ||
  1109.         i.suffix == DWORD_OPCODE_SUFFIX)
  1110.           t->base_opcode |= 0x8;
  1111.       }
  1112.     else if (t->opcode_modifier & Seg2ShortForm)
  1113.       {
  1114.         if (t->base_opcode == POP_SEG_SHORT && i.regs[0]->reg_num == 1)
  1115.           {
  1116.         as_bad ("you can't 'pop cs' on the 386.");
  1117.         return;
  1118.           }
  1119.         t->base_opcode |= (i.regs[0]->reg_num << 3);
  1120.       }
  1121.     else if (t->opcode_modifier & Seg3ShortForm)
  1122.       {
  1123.         /* 'push %fs' is 0x0fa0; 'pop %fs' is 0x0fa1.
  1124.            'push %gs' is 0x0fa8; 'pop %fs' is 0x0fa9.
  1125.            So, only if i.regs[0]->reg_num == 5 (%gs) do we need
  1126.            to change the opcode. */
  1127.         if (i.regs[0]->reg_num == 5)
  1128.           t->base_opcode |= 0x08;
  1129.       }
  1130.     else if (t->opcode_modifier & Modrm)
  1131.       {
  1132.         /* The opcode is completed (modulo t->extension_opcode which must
  1133.            be put into the modrm byte.
  1134.            Now, we make the modrm & index base bytes based on all the info
  1135.            we've collected. */
  1136.  
  1137.         /* i.reg_operands MUST be the number of real register operands;
  1138.            implicit registers do not count. */
  1139.         if (i.reg_operands == 2)
  1140.           {
  1141.         unsigned int source, dest;
  1142.         source = (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 : 1;
  1143.         dest = source + 1;
  1144.         i.rm.mode = 3;
  1145.         /* We must be careful to make sure that all
  1146.            segment/control/test/debug registers go into the i.rm.reg
  1147.            field (despite the whether they are source or destination
  1148.            operands). */
  1149.         if (i.regs[dest]->reg_type & (SReg2 | SReg3 | Control | Debug | Test))
  1150.           {
  1151.             i.rm.reg = i.regs[dest]->reg_num;
  1152.             i.rm.regmem = i.regs[source]->reg_num;
  1153.           }
  1154.         else
  1155.           {
  1156.             i.rm.reg = i.regs[source]->reg_num;
  1157.             i.rm.regmem = i.regs[dest]->reg_num;
  1158.           }
  1159.           }
  1160.         else
  1161.           {            /* if it's not 2 reg operands... */
  1162.         if (i.mem_operands)
  1163.           {
  1164.             unsigned int fake_zero_displacement = 0;
  1165.             unsigned int o = (i.types[0] & Mem) ? 0 : ((i.types[1] & Mem) ? 1 : 2);
  1166.  
  1167.             /* Encode memory operand into modrm byte and base index
  1168.                byte. */
  1169.  
  1170.             if (i.base_reg == esp && !i.index_reg)
  1171.               {
  1172.             /* <disp>(%esp) becomes two byte modrm with no index
  1173.                register. */
  1174.             i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  1175.             i.rm.mode = mode_from_disp_size (i.types[o]);
  1176.             i.bi.base = ESP_REG_NUM;
  1177.             i.bi.index = NO_INDEX_REGISTER;
  1178.             i.bi.scale = 0;    /* Must be zero! */
  1179.               }
  1180.             else if (i.base_reg == ebp && !i.index_reg)
  1181.               {
  1182.             if (!(i.types[o] & Disp))
  1183.               {
  1184.                 /* Must fake a zero byte displacement.  There is
  1185.                    no direct way to code '(%ebp)' directly. */
  1186.                 fake_zero_displacement = 1;
  1187.                 /* fake_zero_displacement code does not set this. */
  1188.                 i.types[o] |= Disp8;
  1189.               }
  1190.             i.rm.mode = mode_from_disp_size (i.types[o]);
  1191.             i.rm.regmem = EBP_REG_NUM;
  1192.               }
  1193.             else if (!i.base_reg && (i.types[o] & BaseIndex))
  1194.               {
  1195.             /* There are three cases here.
  1196.                Case 1:  '<32bit disp>(,1)' -- indirect absolute.
  1197.                (Same as cases 2 & 3 with NO index register)
  1198.                Case 2:  <32bit disp> (,<index>) -- no base register with disp
  1199.                Case 3:  (, <index>)       --- no base register;
  1200.                no disp (must add 32bit 0 disp). */
  1201.             i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  1202.             i.rm.mode = 0;    /* 32bit mode */
  1203.             i.bi.base = NO_BASE_REGISTER;
  1204.             i.types[o] &= ~Disp;
  1205.             i.types[o] |= Disp32;    /* Must be 32bit! */
  1206.             if (i.index_reg)
  1207.               {    /* case 2 or case 3 */
  1208.                 i.bi.index = i.index_reg->reg_num;
  1209.                 i.bi.scale = i.log2_scale_factor;
  1210.                 if (i.disp_operands == 0)
  1211.                   fake_zero_displacement = 1;    /* case 3 */
  1212.               }
  1213.             else
  1214.               {
  1215.                 i.bi.index = NO_INDEX_REGISTER;
  1216.                 i.bi.scale = 0;
  1217.               }
  1218.               }
  1219.             else if (i.disp_operands && !i.base_reg && !i.index_reg)
  1220.               {
  1221.             /* Operand is just <32bit disp> */
  1222.             i.rm.regmem = EBP_REG_NUM;
  1223.             i.rm.mode = 0;
  1224.             i.types[o] &= ~Disp;
  1225.             i.types[o] |= Disp32;
  1226.               }
  1227.             else
  1228.               {
  1229.             /* It's not a special case; rev'em up. */
  1230.             i.rm.regmem = i.base_reg->reg_num;
  1231.             i.rm.mode = mode_from_disp_size (i.types[o]);
  1232.             if (i.index_reg)
  1233.               {
  1234.                 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  1235.                 i.bi.base = i.base_reg->reg_num;
  1236.                 i.bi.index = i.index_reg->reg_num;
  1237.                 i.bi.scale = i.log2_scale_factor;
  1238.                 if (i.base_reg == ebp && i.disp_operands == 0)
  1239.                   {    /* pace */
  1240.                 fake_zero_displacement = 1;
  1241.                 i.types[o] |= Disp8;
  1242.                 i.rm.mode = mode_from_disp_size (i.types[o]);
  1243.                   }
  1244.               }
  1245.               }
  1246.             if (fake_zero_displacement)
  1247.               {
  1248.             /* Fakes a zero displacement assuming that i.types[o]
  1249.                holds the correct displacement size. */
  1250.             exp = &disp_expressions[i.disp_operands++];
  1251.             i.disps[o] = exp;
  1252.             exp->X_seg = absolute_section;
  1253.             exp->X_add_number = 0;
  1254.             exp->X_add_symbol = (symbolS *) 0;
  1255.             exp->X_subtract_symbol = (symbolS *) 0;
  1256.               }
  1257.  
  1258.             /* Select the correct segment for the memory operand. */
  1259.             if (i.seg)
  1260.               {
  1261.             unsigned int seg_index;
  1262.             const seg_entry *default_seg;
  1263.  
  1264.             if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING)
  1265.               {
  1266.                 seg_index = (i.rm.mode << 3) | i.bi.base;
  1267.                 default_seg = two_byte_segment_defaults[seg_index];
  1268.               }
  1269.             else
  1270.               {
  1271.                 seg_index = (i.rm.mode << 3) | i.rm.regmem;
  1272.                 default_seg = one_byte_segment_defaults[seg_index];
  1273.               }
  1274.             /* If the specified segment is not the default, use an
  1275.                opcode prefix to select it */
  1276.             if (i.seg != default_seg)
  1277.               {
  1278.                 if (i.prefixes == MAX_PREFIXES)
  1279.                   {
  1280.                 as_bad ("%d prefixes given and %s segment override gives too many prefixes",
  1281.                     MAX_PREFIXES, i.seg->seg_name);
  1282.                 return;
  1283.                   }
  1284.                 i.prefix[i.prefixes++] = i.seg->seg_prefix;
  1285.               }
  1286.               }
  1287.           }
  1288.  
  1289.         /* Fill in i.rm.reg or i.rm.regmem field with register operand
  1290.            (if any) based on t->extension_opcode. Again, we must be
  1291.            careful to make sure that segment/control/debug/test
  1292.            registers are coded into the i.rm.reg field. */
  1293.         if (i.reg_operands)
  1294.           {
  1295.             unsigned int o =
  1296.             (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 :
  1297.             (i.types[1] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 1 : 2;
  1298.             /* If there is an extension opcode to put here, the
  1299.                register number must be put into the regmem field. */
  1300.             if (t->extension_opcode != None)
  1301.               i.rm.regmem = i.regs[o]->reg_num;
  1302.             else
  1303.               i.rm.reg = i.regs[o]->reg_num;
  1304.  
  1305.             /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
  1306.                we must set it to 3 to indicate this is a register
  1307.                operand int the regmem field */
  1308.             if (!i.mem_operands)
  1309.               i.rm.mode = 3;
  1310.           }
  1311.  
  1312.         /* Fill in i.rm.reg field with extension opcode (if any). */
  1313.         if (t->extension_opcode != None)
  1314.           i.rm.reg = t->extension_opcode;
  1315.           }
  1316.       }
  1317.       }
  1318.   }
  1319.  
  1320.   /* Handle conversion of 'int $3' --> special int3 insn. */
  1321.   if (t->base_opcode == INT_OPCODE && i.imms[0]->X_add_number == 3)
  1322.     {
  1323.       t->base_opcode = INT3_OPCODE;
  1324.       i.imm_operands = 0;
  1325.     }
  1326.  
  1327.   /* We are ready to output the insn. */
  1328.   {
  1329.     register char *p;
  1330.  
  1331.     /* Output jumps. */
  1332.     if (t->opcode_modifier & Jump)
  1333.       {
  1334.     int n = i.disps[0]->X_add_number;
  1335.     segT seg;
  1336.  
  1337.     seg = i.disps[0]->X_seg;
  1338.  
  1339.     if (seg == absolute_section)
  1340.       {
  1341.         if (fits_in_signed_byte (n))
  1342.           {
  1343.         p = frag_more (2);
  1344.         p[0] = t->base_opcode;
  1345.         p[1] = n;
  1346.           }
  1347. #if 0                /* leave out 16 bit jumps - pace */
  1348.         else if (fits_in_signed_word (n))
  1349.           {
  1350.         p = frag_more (4);
  1351.         p[0] = WORD_PREFIX_OPCODE;
  1352.         p[1] = t->base_opcode;
  1353.         md_number_to_chars (&p[2], n, 2);
  1354.           }
  1355. #endif
  1356.         else
  1357.           {            /* It's an absolute dword displacement. */
  1358.         if (t->base_opcode == JUMP_PC_RELATIVE)
  1359.           {        /* pace */
  1360.             /* unconditional jump */
  1361.             p = frag_more (5);
  1362.             p[0] = 0xe9;
  1363.             md_number_to_chars (&p[1], n, 4);
  1364.           }
  1365.         else
  1366.           {
  1367.             /* conditional jump */
  1368.             p = frag_more (6);
  1369.             p[0] = TWO_BYTE_OPCODE_ESCAPE;
  1370.             p[1] = t->base_opcode + 0x10;
  1371.             md_number_to_chars (&p[2], n, 4);
  1372.           }
  1373.           }
  1374.       }
  1375.     else
  1376.       {
  1377.         /* It's a symbol; end frag & setup for relax.
  1378.            Make sure there are more than 6 chars left in the current frag;
  1379.            if not we'll have to start a new one. */
  1380.         if (obstack_room (&frags) <= 6)
  1381.           {
  1382.         frag_wane (frag_now);
  1383.         frag_new (0);
  1384.           }
  1385.         p = frag_more (1);
  1386.         p[0] = t->base_opcode;
  1387.         frag_var (rs_machine_dependent,
  1388.               6,    /* 2 opcode/prefix + 4 displacement */
  1389.               1,
  1390.               ((unsigned char) *p == JUMP_PC_RELATIVE
  1391.                ? ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE)
  1392.                : ENCODE_RELAX_STATE (COND_JUMP, BYTE)),
  1393.               i.disps[0]->X_add_symbol,
  1394.               n, p);
  1395.       }
  1396.       }
  1397.     else if (t->opcode_modifier & (JumpByte | JumpDword))
  1398.       {
  1399.     int size = (t->opcode_modifier & JumpByte) ? 1 : 4;
  1400.     int n = i.disps[0]->X_add_number;
  1401.  
  1402.     if (fits_in_unsigned_byte (t->base_opcode))
  1403.       {
  1404.         FRAG_APPEND_1_CHAR (t->base_opcode);
  1405.       }
  1406.     else
  1407.       {
  1408.         p = frag_more (2);    /* opcode can be at most two bytes */
  1409.         /* put out high byte first: can't use md_number_to_chars! */
  1410.         *p++ = (t->base_opcode >> 8) & 0xff;
  1411.         *p = t->base_opcode & 0xff;
  1412.       }
  1413.  
  1414.     p = frag_more (size);
  1415.     if (i.disps[0]->X_seg == absolute_section)
  1416.       {
  1417.         md_number_to_chars (p, n, size);
  1418.         if (size == 1 && !fits_in_signed_byte (n))
  1419.           {
  1420.         as_bad ("loop/jecx only takes byte displacement; %d shortened to %d",
  1421.             n, *p);
  1422.           }
  1423.       }
  1424.     else
  1425.       {
  1426.         fix_new (frag_now, p - frag_now->fr_literal, size,
  1427.              i.disps[0]->X_add_symbol, i.disps[0]->X_subtract_symbol,
  1428.              i.disps[0]->X_add_number, 1, NO_RELOC);
  1429.       }
  1430.       }
  1431.     else if (t->opcode_modifier & JumpInterSegment)
  1432.       {
  1433.     p = frag_more (1 + 2 + 4);    /* 1 opcode; 2 segment; 4 offset */
  1434.     p[0] = t->base_opcode;
  1435.     if (i.imms[1]->X_seg == absolute_section)
  1436.       md_number_to_chars (p + 1, i.imms[1]->X_add_number, 4);
  1437.     else
  1438.       fix_new (frag_now, p + 1 - frag_now->fr_literal, 4,
  1439.            i.imms[1]->X_add_symbol,
  1440.            i.imms[1]->X_subtract_symbol,
  1441.            i.imms[1]->X_add_number, 0, NO_RELOC);
  1442.     if (i.imms[0]->X_seg != absolute_section)
  1443.       as_bad ("can't handle non absolute segment in long call/jmp");
  1444.     md_number_to_chars (p + 5, i.imms[0]->X_add_number, 2);
  1445.       }
  1446.     else
  1447.       {
  1448.     /* Output normal instructions here. */
  1449.     unsigned char *q;
  1450.  
  1451.     /* First the prefix bytes. */
  1452.     for (q = i.prefix; q < i.prefix + i.prefixes; q++)
  1453.       {
  1454.         p = frag_more (1);
  1455.         md_number_to_chars (p, (unsigned int) *q, 1);
  1456.       }
  1457.  
  1458.     /* Now the opcode; be careful about word order here! */
  1459.     if (fits_in_unsigned_byte (t->base_opcode))
  1460.       {
  1461.         FRAG_APPEND_1_CHAR (t->base_opcode);
  1462.       }
  1463.     else if (fits_in_unsigned_word (t->base_opcode))
  1464.       {
  1465.         p = frag_more (2);
  1466.         /* put out high byte first: can't use md_number_to_chars! */
  1467.         *p++ = (t->base_opcode >> 8) & 0xff;
  1468.         *p = t->base_opcode & 0xff;
  1469.       }
  1470.     else
  1471.       {            /* opcode is either 3 or 4 bytes */
  1472.         if (t->base_opcode & 0xff000000)
  1473.           {
  1474.         p = frag_more (4);
  1475.         *p++ = (t->base_opcode >> 24) & 0xff;
  1476.           }
  1477.         else
  1478.           p = frag_more (3);
  1479.         *p++ = (t->base_opcode >> 16) & 0xff;
  1480.         *p++ = (t->base_opcode >> 8) & 0xff;
  1481.         *p = (t->base_opcode) & 0xff;
  1482.       }
  1483.  
  1484.     /* Now the modrm byte and base index byte (if present). */
  1485.     if (t->opcode_modifier & Modrm)
  1486.       {
  1487.         p = frag_more (1);
  1488.         /* md_number_to_chars (p, i.rm, 1); */
  1489.         md_number_to_chars (p, (i.rm.regmem << 0 | i.rm.reg << 3 | i.rm.mode << 6), 1);
  1490.         /* If i.rm.regmem == ESP (4) && i.rm.mode != Mode 3 (Register mode)
  1491.                    ==> need second modrm byte. */
  1492.         if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING && i.rm.mode != 3)
  1493.           {
  1494.         p = frag_more (1);
  1495.         /* md_number_to_chars (p, i.bi, 1); */
  1496.         md_number_to_chars (p, (i.bi.base << 0 | i.bi.index << 3 | i.bi.scale << 6), 1);
  1497.           }
  1498.       }
  1499.  
  1500.     if (i.disp_operands)
  1501.       {
  1502.         register unsigned int n;
  1503.  
  1504.         for (n = 0; n < i.operands; n++)
  1505.           {
  1506.         if (i.disps[n])
  1507.           {
  1508.             if (i.disps[n]->X_seg == absolute_section)
  1509.               {
  1510.             if (i.types[n] & (Disp8 | Abs8))
  1511.               {
  1512.                 p = frag_more (1);
  1513.                 md_number_to_chars (p, i.disps[n]->X_add_number, 1);
  1514.               }
  1515.             else if (i.types[n] & (Disp16 | Abs16))
  1516.               {
  1517.                 p = frag_more (2);
  1518.                 md_number_to_chars (p, i.disps[n]->X_add_number, 2);
  1519.               }
  1520.             else
  1521.               {    /* Disp32|Abs32 */
  1522.                 p = frag_more (4);
  1523.                 md_number_to_chars (p, i.disps[n]->X_add_number, 4);
  1524.               }
  1525.               }
  1526.             else
  1527.               {        /* not absolute_section */
  1528.             /* need a 32-bit fixup (don't support 8bit non-absolute disps) */
  1529.             p = frag_more (4);
  1530.             fix_new (frag_now, p - frag_now->fr_literal, 4,
  1531.                  i.disps[n]->X_add_symbol, i.disps[n]->X_subtract_symbol,
  1532.                  i.disps[n]->X_add_number, 0, NO_RELOC);
  1533.               }
  1534.           }
  1535.           }
  1536.       }            /* end displacement output */
  1537.  
  1538.     /* output immediate */
  1539.     if (i.imm_operands)
  1540.       {
  1541.         register unsigned int n;
  1542.  
  1543.         for (n = 0; n < i.operands; n++)
  1544.           {
  1545.         if (i.imms[n])
  1546.           {
  1547.             if (i.imms[n]->X_seg == absolute_section)
  1548.               {
  1549.             if (i.types[n] & (Imm8 | Imm8S))
  1550.               {
  1551.                 p = frag_more (1);
  1552.                 md_number_to_chars (p, i.imms[n]->X_add_number, 1);
  1553.               }
  1554.             else if (i.types[n] & Imm16)
  1555.               {
  1556.                 p = frag_more (2);
  1557.                 md_number_to_chars (p, i.imms[n]->X_add_number, 2);
  1558.               }
  1559.             else
  1560.               {
  1561.                 p = frag_more (4);
  1562.                 md_number_to_chars (p, i.imms[n]->X_add_number, 4);
  1563.               }
  1564.               }
  1565.             else
  1566.               {        /* not absolute_section */
  1567.             /* need a 32-bit fixup (don't support 8bit non-absolute ims) */
  1568.             /* try to support other sizes ... */
  1569.             int size;
  1570.             if (i.types[n] & (Imm8 | Imm8S))
  1571.               size = 1;
  1572.             else if (i.types[n] & Imm16)
  1573.               size = 2;
  1574.             else
  1575.               size = 4;
  1576.             p = frag_more (size);
  1577.             fix_new (frag_now, p - frag_now->fr_literal, size,
  1578.                  i.imms[n]->X_add_symbol, i.imms[n]->X_subtract_symbol,
  1579.                  i.imms[n]->X_add_number, 0, NO_RELOC);
  1580.               }
  1581.           }
  1582.           }
  1583.       }            /* end immediate output */
  1584.       }
  1585.  
  1586. #ifdef DEBUG386
  1587.     if (flagseen['D'])
  1588.       {
  1589.     pi (line, &i);
  1590.       }
  1591. #endif /* DEBUG386 */
  1592.  
  1593.   }
  1594.   return;
  1595. }
  1596.  
  1597. /* Parse OPERAND_STRING into the i386_insn structure I.  Returns non-zero
  1598.    on error. */
  1599.  
  1600. static int
  1601. i386_operand (operand_string)
  1602.      char *operand_string;
  1603. {
  1604.   register char *op_string = operand_string;
  1605.  
  1606.   /* Address of '\0' at end of operand_string. */
  1607.   char *end_of_operand_string = operand_string + strlen (operand_string);
  1608.  
  1609.   /* Start and end of displacement string expression (if found). */
  1610.   char *displacement_string_start = NULL;
  1611.   char *displacement_string_end = NULL;
  1612.  
  1613.   /* We check for an absolute prefix (differentiating,
  1614.        for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
  1615.   if (*op_string == ABSOLUTE_PREFIX)
  1616.     {
  1617.       op_string++;
  1618.       i.types[this_operand] |= JumpAbsolute;
  1619.     }
  1620.  
  1621.   /* Check if operand is a register. */
  1622.   if (*op_string == REGISTER_PREFIX)
  1623.     {
  1624.       register reg_entry *r;
  1625.       if (!(r = parse_register (op_string)))
  1626.     {
  1627.       as_bad ("bad register name ('%s')", op_string);
  1628.       return 0;
  1629.     }
  1630.       /* Check for segment override, rather than segment register by
  1631.            searching for ':' after %<x>s where <x> = s, c, d, e, f, g. */
  1632.       if ((r->reg_type & (SReg2 | SReg3)) && op_string[3] == ':')
  1633.     {
  1634.       switch (r->reg_num)
  1635.         {
  1636.         case 0:
  1637.           i.seg = (seg_entry *) & es;
  1638.           break;
  1639.         case 1:
  1640.           i.seg = (seg_entry *) & cs;
  1641.           break;
  1642.         case 2:
  1643.           i.seg = (seg_entry *) & ss;
  1644.           break;
  1645.         case 3:
  1646.           i.seg = (seg_entry *) & ds;
  1647.           break;
  1648.         case 4:
  1649.           i.seg = (seg_entry *) & fs;
  1650.           break;
  1651.         case 5:
  1652.           i.seg = (seg_entry *) & gs;
  1653.           break;
  1654.         }
  1655.       op_string += 4;    /* skip % <x> s : */
  1656.       operand_string = op_string;    /* Pretend given string starts here. */
  1657.       if (!is_digit_char (*op_string) && !is_identifier_char (*op_string)
  1658.           && *op_string != '(' && *op_string != ABSOLUTE_PREFIX)
  1659.         {
  1660.           as_bad ("bad memory operand after segment override");
  1661.           return 0;
  1662.         }
  1663.       /* Handle case of %es:*foo. */
  1664.       if (*op_string == ABSOLUTE_PREFIX)
  1665.         {
  1666.           op_string++;
  1667.           i.types[this_operand] |= JumpAbsolute;
  1668.         }
  1669.       goto do_memory_reference;
  1670.     }
  1671.       i.types[this_operand] |= r->reg_type;
  1672.       i.regs[this_operand] = r;
  1673.       i.reg_operands++;
  1674.     }
  1675.   else if (*op_string == IMMEDIATE_PREFIX)
  1676.     {                /* ... or an immediate */
  1677.       char *save_input_line_pointer;
  1678.       segT exp_seg = 0;
  1679.       expressionS *exp;
  1680.  
  1681.       if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
  1682.     {
  1683.       as_bad ("only 1 or 2 immediate operands are allowed");
  1684.       return 0;
  1685.     }
  1686.  
  1687.       exp = &im_expressions[i.imm_operands++];
  1688.       i.imms[this_operand] = exp;
  1689.       save_input_line_pointer = input_line_pointer;
  1690.       input_line_pointer = ++op_string;    /* must advance op_string! */
  1691.       exp_seg = expression (exp);
  1692.       input_line_pointer = save_input_line_pointer;
  1693.  
  1694.       if (exp_seg == absent_section)
  1695.     {
  1696.       /* missing or bad expr becomes absolute 0 */
  1697.       as_bad ("missing or invalid immediate expression '%s' taken as 0",
  1698.           operand_string);
  1699.       exp->X_seg = absolute_section;
  1700.       exp->X_add_number = 0;
  1701.       exp->X_add_symbol = (symbolS *) 0;
  1702.       exp->X_subtract_symbol = (symbolS *) 0;
  1703.       i.types[this_operand] |= Imm;
  1704.     }
  1705.       else if (exp_seg == absolute_section)
  1706.     {
  1707.       i.types[this_operand] |= smallest_imm_type (exp->X_add_number);
  1708.     }
  1709. #ifndef I386COFF
  1710.       else if (exp_seg != text_section
  1711.            && exp_seg != data_section
  1712.            && exp_seg != bss_section
  1713.            && exp_seg != undefined_section
  1714. #ifdef BFD_ASSEMBLER
  1715.            && ! bfd_is_com_section (exp_seg)
  1716. #endif
  1717.            )
  1718.     {
  1719.     seg_unimplemented:
  1720.       as_bad ("Unimplemented segment type %d in parse_operand", exp_seg);
  1721.       return 0;
  1722.     }
  1723. #endif
  1724.       else
  1725.     {
  1726.       /* this is an address ==> 32bit */
  1727.       i.types[this_operand] |= Imm32;
  1728.     }
  1729.       /* shorten this type of this operand if the instruction wants
  1730.        * fewer bits than are present in the immediate.  The bit field
  1731.        * code can put out 'andb $0xffffff, %al', for example.   pace
  1732.        * also 'movw $foo,(%eax)'
  1733.        */
  1734.       switch (i.suffix)
  1735.     {
  1736.     case WORD_OPCODE_SUFFIX:
  1737.       i.types[this_operand] |= Imm16;
  1738.       break;
  1739.     case BYTE_OPCODE_SUFFIX:
  1740.       i.types[this_operand] |= Imm16 | Imm8 | Imm8S;
  1741.       break;
  1742.     }
  1743.     }
  1744.   else if (is_digit_char (*op_string) || is_identifier_char (*op_string)
  1745.        || *op_string == '(')
  1746.     {
  1747.       /* This is a memory reference of some sort. */
  1748.       register char *base_string;
  1749.       unsigned int found_base_index_form;
  1750.  
  1751.     do_memory_reference:
  1752.       if (i.mem_operands == MAX_MEMORY_OPERANDS)
  1753.     {
  1754.       as_bad ("more than 1 memory reference in instruction");
  1755.       return 0;
  1756.     }
  1757.       i.mem_operands++;
  1758.  
  1759.       /* Determine type of memory operand from opcode_suffix;
  1760.            no opcode suffix implies general memory references. */
  1761.       switch (i.suffix)
  1762.     {
  1763.     case BYTE_OPCODE_SUFFIX:
  1764.       i.types[this_operand] |= Mem8;
  1765.       break;
  1766.     case WORD_OPCODE_SUFFIX:
  1767.       i.types[this_operand] |= Mem16;
  1768.       break;
  1769.     case DWORD_OPCODE_SUFFIX:
  1770.     default:
  1771.       i.types[this_operand] |= Mem32;
  1772.     }
  1773.  
  1774.       /* Check for base index form.  We detect the base index form by
  1775.      looking for an ')' at the end of the operand, searching
  1776.      for the '(' matching it, and finding a REGISTER_PREFIX or ','
  1777.      after it. */
  1778.       base_string = end_of_operand_string - 1;
  1779.       found_base_index_form = 0;
  1780.       if (*base_string == ')')
  1781.     {
  1782.       unsigned int parens_balenced = 1;
  1783.       /* We've already checked that the number of left & right ()'s are
  1784.          equal, so this loop will not be infinite. */
  1785.       do
  1786.         {
  1787.           base_string--;
  1788.           if (*base_string == ')')
  1789.         parens_balenced++;
  1790.           if (*base_string == '(')
  1791.         parens_balenced--;
  1792.         }
  1793.       while (parens_balenced);
  1794.       base_string++;    /* Skip past '('. */
  1795.       if (*base_string == REGISTER_PREFIX || *base_string == ',')
  1796.         found_base_index_form = 1;
  1797.     }
  1798.  
  1799.       /* If we can't parse a base index register expression, we've found
  1800.      a pure displacement expression.  We set up displacement_string_start
  1801.      and displacement_string_end for the code below. */
  1802.       if (!found_base_index_form)
  1803.     {
  1804.       displacement_string_start = op_string;
  1805.       displacement_string_end = end_of_operand_string;
  1806.     }
  1807.       else
  1808.     {
  1809.       char *base_reg_name, *index_reg_name, *num_string;
  1810.       int num;
  1811.  
  1812.       i.types[this_operand] |= BaseIndex;
  1813.  
  1814.       /* If there is a displacement set-up for it to be parsed later. */
  1815.       if (base_string != op_string + 1)
  1816.         {
  1817.           displacement_string_start = op_string;
  1818.           displacement_string_end = base_string - 1;
  1819.         }
  1820.  
  1821.       /* Find base register (if any). */
  1822.       if (*base_string != ',')
  1823.         {
  1824.           base_reg_name = base_string++;
  1825.           /* skip past register name & parse it */
  1826.           while (isalpha (*base_string))
  1827.         base_string++;
  1828.           if (base_string == base_reg_name + 1)
  1829.         {
  1830.           as_bad ("can't find base register name after '(%c'",
  1831.               REGISTER_PREFIX);
  1832.           return 0;
  1833.         }
  1834.           END_STRING_AND_SAVE (base_string);
  1835.           if (!(i.base_reg = parse_register (base_reg_name)))
  1836.         {
  1837.           as_bad ("bad base register name ('%s')", base_reg_name);
  1838.           return 0;
  1839.         }
  1840.           RESTORE_END_STRING (base_string);
  1841.         }
  1842.  
  1843.       /* Now check seperator; must be ',' ==> index reg
  1844.                OR num ==> no index reg. just scale factor
  1845.                OR ')' ==> end. (scale factor = 1) */
  1846.       if (*base_string != ',' && *base_string != ')')
  1847.         {
  1848.           as_bad ("expecting ',' or ')' after base register in `%s'",
  1849.               operand_string);
  1850.           return 0;
  1851.         }
  1852.  
  1853.       /* There may index reg here; and there may be a scale factor. */
  1854.       if (*base_string == ',' && *(base_string + 1) == REGISTER_PREFIX)
  1855.         {
  1856.           index_reg_name = ++base_string;
  1857.           while (isalpha (*++base_string));
  1858.           END_STRING_AND_SAVE (base_string);
  1859.           if (!(i.index_reg = parse_register (index_reg_name)))
  1860.         {
  1861.           as_bad ("bad index register name ('%s')", index_reg_name);
  1862.           return 0;
  1863.         }
  1864.           RESTORE_END_STRING (base_string);
  1865.         }
  1866.  
  1867.       /* Check for scale factor. */
  1868.       if (*base_string == ',' && isdigit (*(base_string + 1)))
  1869.         {
  1870.           num_string = ++base_string;
  1871.           while (is_digit_char (*base_string))
  1872.         base_string++;
  1873.           if (base_string == num_string)
  1874.         {
  1875.           as_bad ("can't find a scale factor after ','");
  1876.           return 0;
  1877.         }
  1878.           END_STRING_AND_SAVE (base_string);
  1879.           /* We've got a scale factor. */
  1880.           if (!sscanf (num_string, "%d", &num))
  1881.         {
  1882.           as_bad ("can't parse scale factor from '%s'", num_string);
  1883.           return 0;
  1884.         }
  1885.           RESTORE_END_STRING (base_string);
  1886.           switch (num)
  1887.         {        /* must be 1 digit scale */
  1888.         case 1:
  1889.           i.log2_scale_factor = 0;
  1890.           break;
  1891.         case 2:
  1892.           i.log2_scale_factor = 1;
  1893.           break;
  1894.         case 4:
  1895.           i.log2_scale_factor = 2;
  1896.           break;
  1897.         case 8:
  1898.           i.log2_scale_factor = 3;
  1899.           break;
  1900.         default:
  1901.           as_bad ("expecting scale factor of 1, 2, 4, 8; got %d", num);
  1902.           return 0;
  1903.         }
  1904.         }
  1905.       else
  1906.         {
  1907.           if (!i.index_reg && *base_string == ',')
  1908.         {
  1909.           as_bad ("expecting index register or scale factor after ','; got '%c'",
  1910.               *(base_string + 1));
  1911.           return 0;
  1912.         }
  1913.         }
  1914.     }
  1915.  
  1916.       /* If there's an expression begining the operand, parse it,
  1917.      assuming displacement_string_start and displacement_string_end
  1918.      are meaningful. */
  1919.       if (displacement_string_start)
  1920.     {
  1921.       register expressionS *exp;
  1922.       segT exp_seg = 0;
  1923.       char *save_input_line_pointer;
  1924.       exp = &disp_expressions[i.disp_operands];
  1925.       i.disps[this_operand] = exp;
  1926.       i.disp_operands++;
  1927.       save_input_line_pointer = input_line_pointer;
  1928.       input_line_pointer = displacement_string_start;
  1929.       END_STRING_AND_SAVE (displacement_string_end);
  1930.       exp_seg = expression (exp);
  1931.       if (*input_line_pointer)
  1932.         as_bad ("Ignoring junk '%s' after expression", input_line_pointer);
  1933.       RESTORE_END_STRING (displacement_string_end);
  1934.       input_line_pointer = save_input_line_pointer;
  1935.       if (exp_seg == absent_section)
  1936.         {
  1937.           /* missing expr becomes absolute 0 */
  1938.           as_bad ("missing or invalid displacement '%s' taken as 0",
  1939.               operand_string);
  1940.           i.types[this_operand] |= (Disp | Abs);
  1941.           exp->X_seg = absolute_section;
  1942.           exp->X_add_number = 0;
  1943.           exp->X_add_symbol = (symbolS *) 0;
  1944.           exp->X_subtract_symbol = (symbolS *) 0;
  1945.         }
  1946.       else if (exp_seg == absolute_section)
  1947.         {
  1948.           i.types[this_operand] |= SMALLEST_DISP_TYPE (exp->X_add_number);
  1949.         }
  1950.       else if (exp_seg == text_section
  1951.            || exp_seg == data_section
  1952.            || exp_seg == bss_section
  1953.            || exp_seg == undefined_section)
  1954.         {
  1955.           i.types[this_operand] |= Disp32;
  1956.         }
  1957.       else
  1958.         {
  1959. #ifdef I386COFF
  1960.           i.types[this_operand] |= Disp32;
  1961. #else
  1962.           goto seg_unimplemented;
  1963. #endif
  1964.         }
  1965.     }
  1966.  
  1967.       /* Make sure the memory operand we've been dealt is valid. */
  1968.       if (i.base_reg && i.index_reg &&
  1969.       !(i.base_reg->reg_type & i.index_reg->reg_type & Reg))
  1970.     {
  1971.       as_bad ("register size mismatch in (base,index,scale) expression");
  1972.       return 0;
  1973.     }
  1974.       /*
  1975.        * special case for (%dx) while doing input/output op
  1976.        */
  1977.       if ((i.base_reg &&
  1978.        (i.base_reg->reg_type == (Reg16 | InOutPortReg)) &&
  1979.        (i.index_reg == 0)))
  1980.     {
  1981.       i.types[this_operand] |= InOutPortReg;
  1982.       return 1;
  1983.     }
  1984.       if ((i.base_reg && (i.base_reg->reg_type & Reg32) == 0) ||
  1985.       (i.index_reg && (i.index_reg->reg_type & Reg32) == 0))
  1986.     {
  1987.       as_bad ("base/index register must be 32 bit register");
  1988.       return 0;
  1989.     }
  1990.       if (i.index_reg && i.index_reg == esp)
  1991.     {
  1992.       as_bad ("%s may not be used as an index register", esp->reg_name);
  1993.       return 0;
  1994.     }
  1995.     }
  1996.   else
  1997.     {                /* it's not a memory operand; argh! */
  1998.       as_bad ("invalid char %s begining %s operand '%s'",
  1999.           output_invalid (*op_string), ordinal_names[this_operand],
  2000.           op_string);
  2001.       return 0;
  2002.     }
  2003.   return 1;            /* normal return */
  2004. }
  2005.  
  2006. /*
  2007.  *            md_estimate_size_before_relax()
  2008.  *
  2009.  * Called just before relax().
  2010.  * Any symbol that is now undefined will not become defined.
  2011.  * Return the correct fr_subtype in the frag.
  2012.  * Return the initial "guess for fr_var" to caller.
  2013.  * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  2014.  * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  2015.  * Although it may not be explicit in the frag, pretend fr_var starts with a
  2016.  * 0 value.
  2017.  */
  2018. int
  2019. md_estimate_size_before_relax (fragP, segment)
  2020.      register fragS *fragP;
  2021.      register segT segment;
  2022. {
  2023.   register unsigned char *opcode;
  2024.   register int old_fr_fix;
  2025.  
  2026.   old_fr_fix = fragP->fr_fix;
  2027.   opcode = (unsigned char *) fragP->fr_opcode;
  2028.   /* We've already got fragP->fr_subtype right;  all we have to do is check
  2029.        for un-relaxable symbols. */
  2030.   if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
  2031.     {
  2032.       /* symbol is undefined in this segment */
  2033.       switch (opcode[0])
  2034.     {
  2035.     case JUMP_PC_RELATIVE:    /* make jmp (0xeb) a dword displacement jump */
  2036.       opcode[0] = 0xe9;    /* dword disp jmp */
  2037.       fragP->fr_fix += 4;
  2038.       fix_new (fragP, old_fr_fix, 4,
  2039.            fragP->fr_symbol,
  2040.            (symbolS *) 0,
  2041.            fragP->fr_offset, 1, NO_RELOC);
  2042.       break;
  2043.  
  2044.     default:
  2045.       /* This changes the byte-displacement jump 0x7N -->
  2046.                the dword-displacement jump 0x0f8N */
  2047.       opcode[1] = opcode[0] + 0x10;
  2048.       opcode[0] = TWO_BYTE_OPCODE_ESCAPE;    /* two-byte escape */
  2049.       fragP->fr_fix += 1 + 4;    /* we've added an opcode byte */
  2050.       fix_new (fragP, old_fr_fix + 1, 4,
  2051.            fragP->fr_symbol,
  2052.            (symbolS *) 0,
  2053.            fragP->fr_offset, 1, NO_RELOC);
  2054.       break;
  2055.     }
  2056.       frag_wane (fragP);
  2057.     }
  2058.   return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
  2059. }                /* md_estimate_size_before_relax() */
  2060.  
  2061. /*
  2062.  *            md_convert_frag();
  2063.  *
  2064.  * Called after relax() is finished.
  2065.  * In:    Address of frag.
  2066.  *    fr_type == rs_machine_dependent.
  2067.  *    fr_subtype is what the address relaxed to.
  2068.  *
  2069.  * Out:    Any fixSs and constants are set up.
  2070.  *    Caller will turn frag into a ".space 0".
  2071.  */
  2072. #ifndef BFD_ASSEMBLER
  2073. void
  2074. md_convert_frag (headers, fragP)
  2075.      object_headers *headers;
  2076.      register fragS *fragP;
  2077. #else
  2078. void
  2079. md_convert_frag (abfd, sec, fragP)
  2080.      bfd *abfd;
  2081.      segT sec;
  2082.      register fragS *fragP;
  2083. #endif
  2084. {
  2085.   register unsigned char *opcode;
  2086.   unsigned char *where_to_put_displacement = NULL;
  2087.   unsigned int target_address;
  2088.   unsigned int opcode_address;
  2089.   unsigned int extension = 0;
  2090.   int displacement_from_opcode_start;
  2091.  
  2092.   opcode = (unsigned char *) fragP->fr_opcode;
  2093.  
  2094.   /* Address we want to reach in file space. */
  2095.   target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
  2096.  
  2097.   /* Address opcode resides at in file space. */
  2098.   opcode_address = fragP->fr_address + fragP->fr_fix;
  2099.  
  2100.   /* Displacement from opcode start to fill into instruction. */
  2101.   displacement_from_opcode_start = target_address - opcode_address;
  2102.  
  2103.   switch (fragP->fr_subtype)
  2104.     {
  2105.     case ENCODE_RELAX_STATE (COND_JUMP, BYTE):
  2106.     case ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE):
  2107.       /* don't have to change opcode */
  2108.       extension = 1;        /* 1 opcode + 1 displacement */
  2109.       where_to_put_displacement = &opcode[1];
  2110.       break;
  2111.  
  2112.     case ENCODE_RELAX_STATE (COND_JUMP, WORD):
  2113.       opcode[1] = TWO_BYTE_OPCODE_ESCAPE;
  2114.       opcode[2] = opcode[0] + 0x10;
  2115.       opcode[0] = WORD_PREFIX_OPCODE;
  2116.       extension = 4;        /* 3 opcode + 2 displacement */
  2117.       where_to_put_displacement = &opcode[3];
  2118.       break;
  2119.  
  2120.     case ENCODE_RELAX_STATE (UNCOND_JUMP, WORD):
  2121.       opcode[1] = 0xe9;
  2122.       opcode[0] = WORD_PREFIX_OPCODE;
  2123.       extension = 3;        /* 2 opcode + 2 displacement */
  2124.       where_to_put_displacement = &opcode[2];
  2125.       break;
  2126.  
  2127.     case ENCODE_RELAX_STATE (COND_JUMP, DWORD):
  2128.       opcode[1] = opcode[0] + 0x10;
  2129.       opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
  2130.       extension = 5;        /* 2 opcode + 4 displacement */
  2131.       where_to_put_displacement = &opcode[2];
  2132.       break;
  2133.  
  2134.     case ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD):
  2135.       opcode[0] = 0xe9;
  2136.       extension = 4;        /* 1 opcode + 4 displacement */
  2137.       where_to_put_displacement = &opcode[1];
  2138.       break;
  2139.  
  2140.     default:
  2141.       BAD_CASE (fragP->fr_subtype);
  2142.       break;
  2143.     }
  2144.   /* now put displacement after opcode */
  2145.   md_number_to_chars ((char *) where_to_put_displacement,
  2146.               displacement_from_opcode_start - extension,
  2147.               SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
  2148.   fragP->fr_fix += extension;
  2149. }
  2150.  
  2151.  
  2152. int md_short_jump_size = 2;    /* size of byte displacement jmp */
  2153. int md_long_jump_size = 5;    /* size of dword displacement jmp */
  2154. int md_reloc_size = 8;        /* Size of relocation record */
  2155.  
  2156. void
  2157. md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
  2158.      char *ptr;
  2159.      long from_addr, to_addr;
  2160.      fragS *frag;
  2161.      symbolS *to_symbol;
  2162. {
  2163.   long offset;
  2164.  
  2165.   offset = to_addr - (from_addr + 2);
  2166.   md_number_to_chars (ptr, (long) 0xeb, 1);    /* opcode for byte-disp jump */
  2167.   md_number_to_chars (ptr + 1, offset, 1);
  2168. }
  2169.  
  2170. void
  2171. md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
  2172.      char *ptr;
  2173.      long from_addr, to_addr;
  2174.      fragS *frag;
  2175.      symbolS *to_symbol;
  2176. {
  2177.   long offset;
  2178.  
  2179.   if (flagseen['m'])
  2180.     {
  2181.       offset = to_addr - S_GET_VALUE (to_symbol);
  2182.       md_number_to_chars (ptr, 0xe9, 1);    /* opcode for long jmp */
  2183.       md_number_to_chars (ptr + 1, offset, 4);
  2184.       fix_new (frag, (ptr + 1) - frag->fr_literal, 4,
  2185.            to_symbol, (symbolS *) 0, (long) 0, 0, NO_RELOC);
  2186.     }
  2187.   else
  2188.     {
  2189.       offset = to_addr - (from_addr + 5);
  2190.       md_number_to_chars (ptr, (long) 0xe9, 1);
  2191.       md_number_to_chars (ptr + 1, offset, 4);
  2192.     }
  2193. }
  2194.  
  2195. int
  2196. md_parse_option (argP, cntP, vecP)
  2197.      char **argP;
  2198.      int *cntP;
  2199.      char ***vecP;
  2200. {
  2201.   return 1;
  2202. }
  2203.  
  2204. void                /* Knows about order of bytes in address. */
  2205. md_number_to_chars (con, value, nbytes)
  2206.      char con[];        /* Return 'nbytes' of chars here. */
  2207.      long value;        /* The value of the bits. */
  2208.      int nbytes;        /* Number of bytes in the output. */
  2209. {
  2210.   register char *p = con;
  2211.  
  2212.   switch (nbytes)
  2213.     {
  2214.     case 1:
  2215.       p[0] = value & 0xff;
  2216.       break;
  2217.     case 2:
  2218.       p[0] = value & 0xff;
  2219.       p[1] = (value >> 8) & 0xff;
  2220.       break;
  2221.     case 4:
  2222.       p[0] = value & 0xff;
  2223.       p[1] = (value >> 8) & 0xff;
  2224.       p[2] = (value >> 16) & 0xff;
  2225.       p[3] = (value >> 24) & 0xff;
  2226.       break;
  2227.     default:
  2228.       BAD_CASE (nbytes);
  2229.     }
  2230. }
  2231.  
  2232.  
  2233. /* Apply a fixup (fixS) to segment data, once it has been determined
  2234.    by our caller that we have all the info we need to fix it up.
  2235.  
  2236.    On the 386, immediates, displacements, and data pointers are all in
  2237.    the same (little-endian) format, so we don't need to care about which
  2238.    we are handling.  */
  2239.  
  2240. static void
  2241. md_apply_fix_1 (fixP, value)
  2242.      fixS *fixP;        /* The fix we're to put in */
  2243.      long value;        /* The value of the bits. */
  2244. {
  2245.   register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
  2246.  
  2247.   switch (fixP->fx_size)
  2248.     {
  2249.     case 1:
  2250.       *p = value;
  2251.       break;
  2252.     case 2:
  2253.       *p++ = value;
  2254.       *p = (value >> 8);
  2255.       break;
  2256.     case 4:
  2257.       *p++ = value;
  2258.       *p++ = (value >> 8);
  2259.       *p++ = (value >> 16);
  2260.       *p = (value >> 24);
  2261.       break;
  2262.     default:
  2263.       BAD_CASE (fixP->fx_size);
  2264.     }
  2265. }
  2266.  
  2267. #ifdef BFD_ASSEMBLER
  2268. int
  2269. md_apply_fix (fixP, valp)
  2270.      fixS *fixP;
  2271.      long *valp;
  2272. {
  2273.   md_apply_fix_1 (fixP, *valp);
  2274.   return 1;
  2275. }
  2276. #else
  2277. void
  2278. md_apply_fix (fixP, val)
  2279.      fixS *fixP;
  2280.      long val;
  2281. {
  2282.   md_apply_fix_1 (fixP, val);
  2283. }
  2284. #endif
  2285.  
  2286. long                /* Knows about the byte order in a word. */
  2287. md_chars_to_number (con, nbytes)
  2288.      unsigned char con[];    /* Low order byte 1st. */
  2289.      int nbytes;        /* Number of bytes in the input. */
  2290. {
  2291.   long retval;
  2292.   for (retval = 0, con += nbytes - 1; nbytes--; con--)
  2293.     {
  2294.       retval <<= BITS_PER_CHAR;
  2295.       retval |= *con;
  2296.     }
  2297.   return retval;
  2298. }
  2299.  
  2300.  
  2301.  
  2302. #define MAX_LITTLENUMS 6
  2303.  
  2304. /* Turn the string pointed to by litP into a floating point constant of type
  2305.    type, and emit the appropriate bytes.  The number of LITTLENUMS emitted
  2306.    is stored in *sizeP .  An error message is returned, or NULL on OK.
  2307.    */
  2308. char *
  2309. md_atof (type, litP, sizeP)
  2310.      char type;
  2311.      char *litP;
  2312.      int *sizeP;
  2313. {
  2314.   int prec;
  2315.   LITTLENUM_TYPE words[MAX_LITTLENUMS];
  2316.   LITTLENUM_TYPE *wordP;
  2317.   char *t;
  2318.  
  2319.   switch (type)
  2320.     {
  2321.     case 'f':
  2322.     case 'F':
  2323.       prec = 2;
  2324.       break;
  2325.  
  2326.     case 'd':
  2327.     case 'D':
  2328.       prec = 4;
  2329.       break;
  2330.  
  2331.     case 'x':
  2332.     case 'X':
  2333.       prec = 5;
  2334.       break;
  2335.  
  2336.     default:
  2337.       *sizeP = 0;
  2338.       return "Bad call to md_atof ()";
  2339.     }
  2340.   t = atof_ieee (input_line_pointer, type, words);
  2341.   if (t)
  2342.     input_line_pointer = t;
  2343.  
  2344.   *sizeP = prec * sizeof (LITTLENUM_TYPE);
  2345.   /* this loops outputs the LITTLENUMs in REVERSE order; in accord with
  2346.        the bigendian 386 */
  2347.   for (wordP = words + prec - 1; prec--;)
  2348.     {
  2349.       md_number_to_chars (litP, (long) (*wordP--), sizeof (LITTLENUM_TYPE));
  2350.       litP += sizeof (LITTLENUM_TYPE);
  2351.     }
  2352.   return 0;
  2353. }
  2354.  
  2355. char output_invalid_buf[8];
  2356.  
  2357. static char *
  2358. output_invalid (c)
  2359.      char c;
  2360. {
  2361.   if (isprint (c))
  2362.     sprintf (output_invalid_buf, "'%c'", c);
  2363.   else
  2364.     sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
  2365.   return output_invalid_buf;
  2366. }
  2367.  
  2368. static reg_entry *
  2369. parse_register (reg_string)
  2370.      char *reg_string;        /* reg_string starts *before* REGISTER_PREFIX */
  2371. {
  2372.   register char *s = reg_string;
  2373.   register char *p;
  2374.   char reg_name_given[MAX_REG_NAME_SIZE];
  2375.  
  2376.   s++;                /* skip REGISTER_PREFIX */
  2377.   for (p = reg_name_given; is_register_char (*s); p++, s++)
  2378.     {
  2379.       *p = register_chars[*s];
  2380.       if (p >= reg_name_given + MAX_REG_NAME_SIZE)
  2381.     return (reg_entry *) 0;
  2382.     }
  2383.   *p = '\0';
  2384.   return (reg_entry *) hash_find (reg_hash, reg_name_given);
  2385. }
  2386.  
  2387.  
  2388. /* We have no need to default values of symbols.  */
  2389.  
  2390. /* ARGSUSED */
  2391. symbolS *
  2392. md_undefined_symbol (name)
  2393.      char *name;
  2394. {
  2395.   return 0;
  2396. }
  2397.  
  2398. /* Parse an operand that is machine-specific.
  2399.    We just return without modifying the expression if we have nothing
  2400.    to do.  */
  2401.  
  2402. /* ARGSUSED */
  2403. void
  2404. md_operand (expressionP)
  2405.      expressionS *expressionP;
  2406. {
  2407. }
  2408.  
  2409. /* Round up a section size to the appropriate boundary.  */
  2410. long
  2411. md_section_align (segment, size)
  2412.      segT segment;
  2413.      long size;
  2414. {
  2415.   return size;            /* Byte alignment is fine */
  2416. }
  2417.  
  2418. /* Exactly what point is a PC-relative offset relative TO?
  2419.    On the i386, they're relative to the address of the offset, plus
  2420.    its size. (??? Is this right?  FIXME-SOON!) */
  2421. long
  2422. md_pcrel_from (fixP)
  2423.      fixS *fixP;
  2424. {
  2425.   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
  2426. }
  2427.  
  2428. #ifndef I386COFF
  2429.  
  2430. static void
  2431. s_bss ()
  2432. {
  2433.   register int temp;
  2434.  
  2435.   temp = get_absolute_expression ();
  2436. #ifdef BFD_ASSEMBLER
  2437.   subseg_set (bss_section, (subsegT) temp);
  2438. #else
  2439.   subseg_new (bss_section, (subsegT) temp);
  2440. #endif
  2441.   demand_empty_rest_of_line ();
  2442. }
  2443.  
  2444. #endif
  2445.  
  2446.  
  2447. #ifdef BFD_ASSEMBLER
  2448.  
  2449. arelent *
  2450. tc_gen_reloc (section, fixp)
  2451.      asection *section;
  2452.      fixS *fixp;
  2453. {
  2454.   arelent *reloc;
  2455.   bfd_reloc_code_real_type code;
  2456.  
  2457. #define F(SZ,PCREL)        (((SZ) << 1) + (PCREL))
  2458.   switch (F (fixp->fx_size, fixp->fx_pcrel))
  2459.     {
  2460. #define MAP(SZ,PCREL,TYPE)    case F(SZ,PCREL): code = (TYPE); break
  2461.       MAP (1, 0, BFD_RELOC_8);
  2462.       MAP (2, 0, BFD_RELOC_16);
  2463.       MAP (4, 0, BFD_RELOC_32);
  2464.       MAP (1, 1, BFD_RELOC_8_PCREL);
  2465.       MAP (2, 1, BFD_RELOC_16_PCREL);
  2466.       MAP (4, 1, BFD_RELOC_32_PCREL);
  2467.     default:
  2468.       abort ();
  2469.     }
  2470.  
  2471.   reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
  2472.   assert (reloc != 0);
  2473.   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
  2474.   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
  2475.   if (fixp->fx_pcrel)
  2476.     reloc->addend = fixp->fx_addnumber;
  2477.   else
  2478.     reloc->addend = 0;
  2479.  
  2480.   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
  2481.   assert (reloc->howto != 0);
  2482.  
  2483.   return reloc;
  2484. }
  2485.  
  2486. #else /* ! BFD_ASSEMBLER */
  2487.  
  2488. #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
  2489. void
  2490. tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
  2491.      char *where;
  2492.      fixS *fixP;
  2493.      relax_addressT segment_address_in_file;
  2494. {
  2495.   /*
  2496.    * In: length of relocation (or of address) in chars: 1, 2 or 4.
  2497.    * Out: GNU LD relocation length code: 0, 1, or 2.
  2498.    */
  2499.  
  2500.   static unsigned char nbytes_r_length[] =
  2501.   {42, 0, 1, 42, 2};
  2502.   long r_symbolnum;
  2503.  
  2504.   know (fixP->fx_addsy != NULL);
  2505.  
  2506.   md_number_to_chars (where,
  2507.        fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
  2508.               4);
  2509.  
  2510.   r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
  2511.          ? S_GET_TYPE (fixP->fx_addsy)
  2512.          : fixP->fx_addsy->sy_number);
  2513.  
  2514.   where[6] = (r_symbolnum >> 16) & 0x0ff;
  2515.   where[5] = (r_symbolnum >> 8) & 0x0ff;
  2516.   where[4] = r_symbolnum & 0x0ff;
  2517.   where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
  2518.           | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
  2519.           | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
  2520. }
  2521.  
  2522. #endif /* OBJ_AOUT or OBJ_BOUT */
  2523.  
  2524. #if defined (I386COFF)
  2525.  
  2526. short
  2527. tc_coff_fix2rtype (fixP)
  2528.      fixS *fixP;
  2529. {
  2530.   return (fixP->fx_pcrel ?
  2531.       (fixP->fx_size == 1 ? R_PCRBYTE :
  2532.        fixP->fx_size == 2 ? R_PCRWORD :
  2533.        R_PCRLONG) :
  2534.       (fixP->fx_size == 1 ? R_RELBYTE :
  2535.        fixP->fx_size == 2 ? R_RELWORD :
  2536.        R_DIR32));
  2537.  
  2538.  
  2539. }
  2540.  
  2541. int
  2542. tc_coff_sizemachdep (frag)
  2543.      fragS *frag;
  2544. {
  2545.   if (frag->fr_next)
  2546.     return (frag->fr_next->fr_address - frag->fr_address);
  2547.   else
  2548.     return 0;
  2549. }
  2550.  
  2551. #endif /* I386COFF */
  2552.  
  2553. #endif /* BFD_ASSEMBLER? */
  2554.  
  2555. /* end of tc-i386.c */
  2556.